<template>
  <div class="content-item">
    <TableModel :table-data="groupList" :loading="loading" :is-show-select=false>
      <el-table-column prop="name" label="班组名称"></el-table-column>
      <el-table-column prop="depName" label="所属组织"></el-table-column>
      <el-table-column prop="bidSectionName" label="标段"></el-table-column>
      <el-table-column prop="areaName" label="所属工区"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </TableModel>
    <Pagination :pageData="pageData" @pageNum="changPageNum" @pageSize="onChangePageSize" />
  </div>
</template>

<script>
import Pagination from "../Pagination";
import TableModel from "../../models/TableModel";
import { messageBox } from "../../../util/utils";
import HttpInterface from "../../../util/httpInterface";

export default {
  name: "GroupList",
  components: {
    Pagination, TableModel
  },
  data() {
    return {
      loading: false,
      organization: "",
      multipleSelection: [],
      pageData: {
        total: 0,
        pageNum: 1,
        pageSize: 20
      },
      groupList: []
    };
  },

  methods: {
    // 获取班组列表
    getGroupList () {
      this.loading = true;
      var _this = this;
      var params = {
        pageSize: this.pageData.pageSize,
        pageNum: this.pageData.pageNum
      };
      HttpInterface.requestPost(this, HttpInterface.URL_GROUP_LIST, params, function (res) {
        var { code, data, total } = res.data;
        data.forEach(item => {
          let str = "";
          if (item.workAreaList.length > 0) {
            item.workAreaList.forEach(key => {
              str += key.name + "、";
            })
            item.areaName = str.slice(0, -1);
          }
        })
        _this.groupList = data.reverse();
        _this.pageData.total = total;
        _this.loading = false;
      });
    },

    changPageNum: function (d) {
      this.pageData.pageNum = d;
      this.getGroupList()
    },

    onChangePageSize: function (d) {
      this.pageData.pageSize = d;
      this.getGroupList();
    },

    handleEdit(row) {
      this.$emit("modifGroup", row)
    },

    handleDelete(row) {
      var _this = this;
      messageBox(() => {
        var params = {
          groupid: row.id
        };
        HttpInterface.requestPost(this, HttpInterface.URL_GROUP_DELGROUP, params, function (res) {
          var data = res.data;
          if (data.success) {
            _this.successMessage("删除成功！");
            _this.getGroupList();
          } else {
            _this.errorMessage(data.msg);
          }
        }, null, true);
      })
    }
  }
};
</script>

<style  scoped>
</style>
